package com.jh3502.vivo.function.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/11 10:10
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class CarTypeDialog extends BaseActivity<CarTypePresenter.CarTypeView, CarTypePresenter>
        implements CarTypePresenter.CarTypeView {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.cancel)
    TextView cancel;
    @BindView(R.id.submit)
    TextView submit;
    List<CarTypeBean> dataList = new ArrayList<>(2);
    private String name;
    private int type;


    @Override
    public CarTypePresenter initPresenter() {
        return new CarTypePresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_reim_type_choose;
    }

    @Override
    protected void initView() {
        mPresenter.setCarTypeView(this);
        mPresenter.toFindTraffic();
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        toolbarTitle.setText("车型选择");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void onSubmitClicked() {
        if (TextUtils.isEmpty(name)) {
            showShortToast("最少选择一项");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("name", name);
        intent.putExtra("type", type);
        intent.putExtra("positionMain", getIntent().getIntExtra("positionMain", -1));
        intent.putExtra("typeItem", getIntent().getIntExtra("typeItem", -1));
        intent.putExtra("positionItem", getIntent().getIntExtra("positionItem", -1));
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }

    @Override
    public void findTrafficSuccess(List<CarTypeBean> datas) {
        //    1:动车 2：高铁 3：火车 4：轮船 5：飞机 6：长途客车
        List<CarTypeBean> data1 = new ArrayList<>(2);
        List<CarTypeBean> data2 = new ArrayList<>(2);
        List<CarTypeBean> data3 = new ArrayList<>(2);
        List<CarTypeBean> data4 = new ArrayList<>(2);
        List<CarTypeBean> data5 = new ArrayList<>(2);
        List<CarTypeBean> data6 = new ArrayList<>(2);
        CarTypeBean cbRoot1 = new CarTypeBean();
        CarTypeBean cbRoot2 = new CarTypeBean();
        CarTypeBean cbRoot3 = new CarTypeBean();
        CarTypeBean cbRoot4 = new CarTypeBean();
        CarTypeBean cbRoot5 = new CarTypeBean();
        CarTypeBean cbRoot6 = new CarTypeBean();
        cbRoot1.setName("动车");
        cbRoot1.setRoot(true);
        cbRoot2.setName("高铁");
        cbRoot2.setRoot(true);
        cbRoot3.setName("火车");
        cbRoot3.setRoot(true);
        cbRoot4.setName("轮船");
        cbRoot4.setRoot(true);
        cbRoot5.setName("飞机");
        cbRoot5.setRoot(true);
        cbRoot6.setName("长途客车");
        cbRoot6.setRoot(true);
        data1.add(cbRoot1);
        data2.add(cbRoot2);
        data3.add(cbRoot3);
        data4.add(cbRoot4);
        data5.add(cbRoot5);
        data6.add(cbRoot6);

        for (CarTypeBean bean : datas) {
            if (bean.getType() == 1) {
                data1.add(bean);
            } else if (bean.getType() == 2) {
                data2.add(bean);
            } else if (bean.getType() == 3) {
                data3.add(bean);
            } else if (bean.getType() == 4) {
                data4.add(bean);
            } else if (bean.getType() == 5) {
                data5.add(bean);
            } else if (bean.getType() == 6) {
                data6.add(bean);
            }
        }

        dataList.addAll(data1);
        dataList.addAll(data2);
        dataList.addAll(data3);
        dataList.addAll(data4);
        dataList.addAll(data5);
        dataList.addAll(data6);

        ListChoiceAdapter adapter = new ListChoiceAdapter(this, dataList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                name = dataList.get(position).getName();
                type = dataList.get(position).getSmallType();
                adapter.notifyDataSetChanged();
            }
        });

    }


    private class ListChoiceAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<CarTypeBean> list;

        public ListChoiceAdapter(Context context, List<CarTypeBean> list) {
            this.inflater = LayoutInflater.from(context);
            this.list = list;


        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListChoiceAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_reim_type_choice, null);
                holder = new ListChoiceAdapter.ViewHolder();
                holder.root = convertView.findViewById(R.id.root);
                holder.blank = convertView.findViewById(R.id.blank);
                holder.title = convertView.findViewById(R.id.title);
                holder.check = convertView.findViewById(R.id.check);

                convertView.setTag(holder);
            } else {
                holder = (ListChoiceAdapter.ViewHolder) convertView.getTag();
            }
            boolean check = ((ListView) parent).isItemChecked(position);
            holder.check.setChecked(check);
            holder.title.setText(dataList.get(position).getName());
            boolean isRoot = dataList.get(position).isRoot();
            if (isRoot) {
                holder.root.setBackgroundColor(Color.parseColor("#FFE2E7E9"));
                holder.blank.setVisibility(View.GONE);
                holder.title.setTextColor(Color.parseColor("#FF333333"));
                holder.check.setVisibility(View.GONE);
            } else {
                holder.root.setBackgroundColor(Color.WHITE);
                holder.blank.setVisibility(View.VISIBLE);
                holder.title.setTextColor(Color.parseColor("#FF666666"));
                holder.check.setVisibility(View.VISIBLE);
            }
            return convertView;
        }

        private class ViewHolder {
            TextView title;
            View blank;
            ConstraintLayout root;
            CheckBox check;

        }
    }
}
